<?php
namespace App\Admin\Extensions\Tools;

use Encore\Admin\Admin;

class UserTools
{
	protected $userid;
	protected $action;//要操作的状态
	protected $user_stutus;//用户当前状态
	protected $to_user_stutus;//改变后的状态
	
	public function __construct($userid, $user_stutus, $action)
	{
		$this->userid= $userid;
		$this->action = $action;
		$this->user_stutus = $user_stutus == 'enabled' ? '禁用' : '启用';
		$this->to_user_stutus = $action == 'enabled' ? '禁用' : '启用';
	}
	
	protected function script()
	{
		$url = admin_base_path('member/usertools');
		
		return <<<SCRIPT

$('.grid-user-action').unbind('click').click(function() {

    var id = $(this).data('id');
	var action = $(this).data('action');
	var to_user_stutus = $(this).text();

    swal({
    title: "确认" + to_user_stutus + "?",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "确认",
      closeOnConfirm: false,
      cancelButtonText: "取消"
    },
    function(){
        $.ajax({
            method: 'post',
            url: "$url",
            data: {
                _token:LA.token,
				userid: id,
				action: action,
            },
            success: function (data) {
                $.pjax.reload('#pjax-container');

                
				if (typeof data === 'object') {
                    if (data.status) {
                        swal(data.message, '', 'success');
                    } else {
                        swal(data.message, '', 'error');
                    }
                }
            }
        });
    });
});

SCRIPT;
	}
	
	protected function render()
	{
		Admin::script($this->script());
		
		return "<a class='btn btn-xs grid-user-action' data-id='{$this->userid}' style='color:red' data-action='{$this->action}'>$this->user_stutus</a>";
	}
	
	public function __toString()
	{
		return $this->render();
	}
}